package com.example.remword;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.*;

@SpringBootApplication
public class RemwordApplication implements CommandLineRunner {

    @Autowired
    private RememberWordMapper rememberWordMapper;
    @Autowired
    private InitWord initWord;

    public static void main(String[] args) {
        SpringApplication.run(RemwordApplication.class, args);
    }

    List<Word> wordList;
    Set<Word> errorWordList = new HashSet<>();
    Scanner input = new Scanner(System.in);

    @Override
    public void run(String... args) throws Exception {

        // 定义全局变量缓存单词列表
        wordList = rememberWordMapper.selectAll();

        menu();

        while (true) {
            switch (input.next().trim()) {
                case "1":
                    writeWordEn();
                    menu();
                    break;
                case "2":
                    writeWordCn();
                    menu();
                    break;
                case "3":
                    selectAllWord();
                    break;
                case "4":
                    errorWordRecord();
                    break;
              /*  case "5":
                    initWord.initWord();
                    break;*/
                default:
                    System.out.println("程序结束！");
                    System.exit(-1);
            }

        }

    }

    private void menu() {
        System.out.println("\n\n欢迎来到单词记忆系统！\n");
        System.out.println("选择数字进入相应的功能，非数字退出程序\n");
        System.out.println("1、根据中文提示写单词\n");
        System.out.println("2、根据英文写中文\n");
        System.out.println("3、查看所有单词\n");
        System.out.println("4、错题记录\n");
//        System.out.println("5、初始化\n");
    }


    private void selectAllWord() {
        // 选择查看单词既能查看单词，也会重新刷新全局Word列表
        wordList = rememberWordMapper.selectAll();
        for (Word word : wordList) {
            System.out.println(WordStrUtils.getStandard(word.getId()) + WordStrUtils.getStandard(word.getWordEn()) +  WordStrUtils.getStandard(word.getWordCn()) );
        }
    }

    private void writeWordEn() {

        while (true) {
            int random = (int) (Math.random() * (wordList.size()));
            Word word = wordList.get(random);

            System.out.println("\n\n中文为\"\t" + word.getWordCn() + "\t\"，的英文是什么？");
            String wordEn = input.next().trim();
            if (wordEn.equals(word.getWordEn())) {
                this.removeErrorWord(word);
                System.out.println("恭喜您，答对了！准备接好下一题哦！");

            } else {
                this.addErrorWord(word);
                System.out.println("答题错误！正确答案为\t"+word.getWordEn()+"\n按数字1返回上一级，其他键继续作答");
                String next = input.next();
                if (next.equals("1")) {
                    break;
                }
            }
        }

    }

    private void writeWordCn() {
        while (true) {
            int random = (int) (Math.random() * (wordList.size()));
            Word word = wordList.get(random);

            System.out.println("\n\n英文为\"\t" + word.getWordEn() + "\t\"，的中文是什么？");
            String wordCn = input.next().trim();
            if (word.getWordCn().contains(wordCn)) {
                this.removeErrorWord(word);
                System.out.println("恭喜您，答对了！准备接好下一题哦！");

            } else {
                this.addErrorWord(word);
                System.out.println("答题错误！正确答案为\t"+word.getWordCn()+"\n按数字1返回上一级，其他键继续作答");
                String next = input.next();
                if (next.equals("1")) {
                    break;
                }
            }
        }
    }

    private void addErrorWord(Word word){
        errorWordList.add(word);
    }

    private void removeErrorWord(Word word){
        errorWordList.remove(word);
    }

    private void errorWordRecord(){
        for (Word word : errorWordList) {
            System.out.println("\t" + WordStrUtils.getStandard(word.getId()) + "\t\t\t" + WordStrUtils.getStandard(word.getWordEn()) + "\t\t\t" + WordStrUtils.getStandard(word.getWordCn()) + "\t");
        }
    }

}
